package net.samsb.tool;

import android.os.Handler;

import java.lang.ref.WeakReference;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Destiny_hao on 2017/4/14.
 */

public class MyHandlerManager {

    private static MyHandlerManager sInstance;

    private Map<String, WeakReference<Handler>> sHandlerWeakRef = new HashMap<>();

    public MyHandlerManager() {

    }

    public static MyHandlerManager getInstance() {
        return sInstance;
    }

    public Handler getHandlerWithName(String key) {
        Handler handler = null;

        if (sHandlerWeakRef.get(key) != null) {
            handler = sHandlerWeakRef.get(key).get();
        }
        return handler;
    }

    public void setHandlerWithName(String key, Handler handler) {
        sHandlerWeakRef.put(key, new WeakReference<Handler>(handler));
    }

    public void clran() {
        sHandlerWeakRef.clear();
    }
}